package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.BasicPopmodule;

public class PopEvaluateModule extends BasicPopmodule {

    private Context mContext;
    private PopEvaluateListener mPoplistener;

    private int mGrade;
    private String mTitle;

    public PopEvaluateModule(Context context, PopEvaluateListener l) {
        this.mContext = context;
        this.mPoplistener = l;
        initView();
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_evaluate_module, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener() {
        View mVwPopBlank = (View) mContentView.findViewById(R.id.vw_pop_blank);
        TextView mTvOrderStartsCancelEvaluate = (TextView) mContentView.findViewById(R.id.tv_order_starts_cancel_evaluate);
        final RatingBar mRbOrderDetailsEvaluate = (RatingBar) mContentView.findViewById(R.id.rb_order_details_evaluate);
        final TextView mTvOrderDetailsEvaluateTitle1 = (TextView) mContentView.findViewById(R.id.tv_order_details_evaluate_title1);
        final TextView mTvOrderDetailsEvaluateTitle2 = (TextView) mContentView.findViewById(R.id.tv_order_details_evaluate_title2);
        final TextView mTvOrderDetailsEvaluateTitle3 = (TextView) mContentView.findViewById(R.id.tv_order_details_evaluate_title3);
        final TextView mTvOrderDetailsEvaluateTitle4 = (TextView) mContentView.findViewById(R.id.tv_order_details_evaluate_title4);
        TextView mTvEvaluateSubmission = (TextView) mContentView.findViewById(R.id.tv_evaluate_submission);
        mVwPopBlank.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
        mTvOrderStartsCancelEvaluate.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
        mTvOrderDetailsEvaluateTitle1.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mTitle=mTvOrderDetailsEvaluateTitle1.getText().toString();
                mTvOrderDetailsEvaluateTitle1.setBackgroundResource(R.drawable.shap_order_details_evalueta_check);
                mTvOrderDetailsEvaluateTitle2.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle3.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle4.setBackgroundResource(R.drawable.shap_order_details_evalueta);
            }
        });
        mTvOrderDetailsEvaluateTitle2.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mTitle=mTvOrderDetailsEvaluateTitle2.getText().toString();
                mTvOrderDetailsEvaluateTitle1.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle2.setBackgroundResource(R.drawable.shap_order_details_evalueta_check);
                mTvOrderDetailsEvaluateTitle3.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle4.setBackgroundResource(R.drawable.shap_order_details_evalueta);
            }
        });
        mTvOrderDetailsEvaluateTitle3.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mTitle=mTvOrderDetailsEvaluateTitle3.getText().toString();
                mTvOrderDetailsEvaluateTitle1.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle2.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle3.setBackgroundResource(R.drawable.shap_order_details_evalueta_check);
                mTvOrderDetailsEvaluateTitle4.setBackgroundResource(R.drawable.shap_order_details_evalueta);
            }
        });
        mTvOrderDetailsEvaluateTitle4.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mTitle=mTvOrderDetailsEvaluateTitle4.getText().toString();
                mTvOrderDetailsEvaluateTitle1.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle2.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle3.setBackgroundResource(R.drawable.shap_order_details_evalueta);
                mTvOrderDetailsEvaluateTitle4.setBackgroundResource(R.drawable.shap_order_details_evalueta_check);
            }
        });
        mTvEvaluateSubmission.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mGrade=mRbOrderDetailsEvaluate.getNumStars();
                if (TextUtils.isEmpty(mTitle)||"".equals(mTitle)){
                    Toast toast = Toast.makeText(mContext, "请选择您要评价的内容", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast, 3000);
                    return;
                }
                mPoplistener.getEvaluate(mGrade*20,mTitle);
                mPopwindows.dismiss();
            }
        });
    }

    public interface PopEvaluateListener {

        void getEvaluate(int grade, String title);
    }
}
